package com.mes.common.service;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.manager.iface.IBasicManager;
import com.mes.common.util.SysUtil;
import com.mes.common.dao.iface.IBasicDao;

@Service
@SuppressWarnings(value={"unchecked","rawtypes", "unused"})
public class BasicService<T>{
	@Autowired
	SysUtil sysUtil;
	
	@Autowired
	private IBasicManager<T> basicManager;
	
	@Autowired
	private IBasicDao<T> basicDao;

	@Transactional
	public void saveAll(Collection<T> collection) throws Exception {
		for (T t : collection) {
			switch (EntityUtils.getState(t)) {
			case MODIFIED:
				basicDao.update(t);
				break;
			case DELETED:
				basicDao.delete(t);
				break;
			case NEW:
				basicDao.add(t);
				break;
			default:
				break;
			}
		};
		
	}
	
	public void saveData(Collection collection) throws Exception {
		this.saveAll(collection);
	}
	
	@DataResolver
	public void saveAll(Map<String, Collection> dataItems) throws Exception {
		for (Iterator iterator = dataItems.values().iterator(); iterator.hasNext();) {
			Collection collection = (Collection)iterator.next();
			this.saveAll(collection);
		}
	}
	
	@DataProvider
	public void getAll(Page page) throws Exception {
		basicDao.loadAll(page);
	}
	
	@DataProvider
	public Collection getAll() throws Exception {
		Collection collection = basicDao.loadAll();
		return collection;
	}

	/**
	 * 获得报表服务器的路径
	 * @return
	 * @throws Exception
	 */
	@Expose
	public String getReportURL() throws Exception {
		return sysUtil.getProperty("reportURL");
	}
	
	/**
	 * 获得configInfo.properties文件中定义的某个系统参数的值
	 * @param propertyName
	 * @return
	 * @throws Exception
	 */
	@Expose
	public String getProperty(String propertyName) throws Exception {
		return sysUtil.getProperty(propertyName);
	}
	
	/**
	 * 获得一个UUID值
	 * @return
	 * @throws Exception
	 */
	@Expose
	public String getUUID() throws Exception {
		return SysUtil.getUUID();
	}
}
